<template>
  <div class="sf-tab" :class="{ leftTab: tabPosition === 'left' }">
    <div v-show="!showBack" class="tab-item line-right" :style="{ 'line-height': lineHeihgt }" @click="goBack">
      <i class="el-icon-arrow-left" />
      返回
    </div>
    <div v-if="fromCusSummary" :style="{ 'line-height': lineHeihgt }" class="cusType">
      <span v-if="cusTypeTitle" class="cusType-title">{{ cusTypeTitle }}</span>
      <el-dropdown trigger="click" size="medium" @command="changeCusType">
        <span class="el-dropdown-link cursor">
          {{ cusType }}
          <i class="el-icon-arrow-down el-icon--right" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="(item, index) in fromList" :key="index" :command="item">{{ item }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div v-if="fromCusSummaryRight" :style="{ 'line-height': lineHeihgt }" class="cusTypeRight">
      <el-dropdown trigger="click" size="medium" @command="changeCusTypeRight">
        <span class="el-dropdown-link cursor">
          {{ cusTypeRight }}
          <i class="el-icon-arrow-down el-icon--right" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="(item, index) in fromListRight" :key="index" :command="item">{{ item }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div
      v-for="item in tabs"
      v-show="showTab !== 'hide'"
      :key="item.value"
      :style="{ 'line-height': lineHeihgt }"
      class="tab-item"
      :class="{ 'tab-active': activeName == item.value }"
      @click="handleClick(item.value, item)"
    >
      <template v-if="item.textType === 'badge' && (item.zeroShow || (!item.zeroShow && item.num > 0))">
        <el-badge :value="item.num" :max="item.max || 9999" class="badge-item">
          <span>{{ item.name }}</span>
        </el-badge>
      </template>
      <template v-else>
        {{ item.name }}
        <span v-if="item.num">({{ item.num }})</span>
      </template>
    </div>
    <div class="sf-btn">
      <slot />
    </div>
  </div>
</template>
<script>
export default {
  props: {
    tabs: {
      type: Array,
      default: () => []
    },
    activeName: {
      type: String | Number,
      default: null
    },
    // 是否显示返回按钮
    showBack: {
      type: Boolean
    },
    // 是否显示tab
    showTabs: {
      type: String,
      default: 'normal'
    },
    cusTypeTitle: {
      type: String,
      default: ''
    },
    // 对齐方式
    tabPosition: {
      type: String,
      default: ''
    },
    // 是否显示左边下拉
    fromCusSummary: {
      type: Boolean,
      default: false
    },
    // 是否显示右边下拉
    fromCusSummaryRight: {
      type: Boolean,
      default: false
    },
    // 左侧下拉数据
    fromList: {
      type: Array,
      default: () => []
    },
    // 右侧下拉数据
    fromListRight: {
      type: Array,
      default: () => []
    },
    // 点击是否返回所有数据
    isAllReturn: {
      type: Boolean,
      default: false
    },
    lineHeihgt: {
      type: String,
      default: ''
    },
    // 是否显示红点
    textType: {
      type: String,
      default: ''
    },
    leftFlexStyle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      cusType: '',
      cusTypeRight: '',
      showTab: 'normal'
    }
  },
  watch: {
    fromList: {
      handler(v) {
        if (v) {
          this.cusType = v[0]
        }
      },
      immediate: true
    },
    fromListRight: {
      handler(v) {
        if (v) {
          this.cusTypeRight = v[0]
        }
      },
      immediate: true
    },
    showTabs: {
      handler(v) {
        this.showTab = v
      },
      immediate: true
    }
  },
  methods: {
    handleClick(v, data) {
      if (this.isAllReturn) {
        this.$emit('handleTabClick', {
          activeName: v,
          data
        })
      } else {
        this.$emit('handleTabClick', v)
      }
    },
    goBack() {
      this.$emit('handleBack')
    },
    changeCusType(v) {
      this.cusType = v
      this.$emit('handelCusType', v)
    },
    changeCusTypeRight(v) {
      this.cusTypeRight = v
      this.$emit('handelCusTypeRight', v)
    }
  }
}
</script>
<style lang="scss" scoped>
.sf-tab {
  min-height: 50px;
  color: #333;
  background-color: #fff;
  padding: 0 15px;
  background-color: #ffffff;
  box-shadow: 0px 0px 6px 0px rgb(0 0 0 / 8%);
  border-radius: 6px;
  margin-bottom: 6px;
  overflow: hidden;
  .tab-item {
    line-height: 53px;
    display: inline-block;
    margin: 0 15px;
    cursor: pointer;
    position: relative;
    font-size: 16px;
    letter-spacing: 0px;
    color: #727373;
    .el-icon-back {
      font-size: 17px;
      font-weight: bold;
      margin-right: 10px;
    }
    &.line-right::after {
      content: '';
      position: absolute;
      right: -20px;
      top: 12px;
      height: 40px;
      width: 1px;
      background-color: #ebeef5;
      border: none;
      // transform: scaleX(.5);
    }
    &:hover {
      color: #409EFF;
    }
  }
  .tab-active {
    // color: #409EFF;
    color: rgba(0,0,0,0.88);
    font-weight: 700;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 50%;
      bottom: 0;
      width: 20px;
      transform: translate(-50%, 0);
      border-bottom: 5px solid #409EFF;
    }
  }
  .sf-btn {
    float: right;
    line-height: 50px;
  }
}
.leftTab {
  .tab-item {
    display: block;
    text-align: center;
  }
}
.cusType {
  display: inline-block;
  line-height: 53px;
  .el-dropdown {
    font-size: 16px;
  }
  .cusType-title {
    font-size: 16px;
  }
}
.cusTypeRight {
  display: inline-block;
  float: right;
  line-height: 53px;
  .el-dropdown {
    font-size: 16px;
  }
}
.badge-item {
  ::v-deep {
    .el-badge__content {
      top: 15px;
    }
  }
}
</style>
